var url = appPath + "/area/getAreas.html";
var ohtml = "<option value='0' selected='selected'>请选择</option>";
function tc_center_center(id) {
	var _top = (screen.height - $("#" + id).height()) / 2 + $(window).scrollTop() - 85;
	var _left = ($(window).width() - $("#" + id).width()) / 2;
	$("#" + id).css({
		display:'block',
		top : _top,
		left : _left
	});
}
//加载省份
function loadProvince(callback) {
	$.ajax({
		type : "POST",
		dataType : "json",
		url : url,
		data : {
			parentId : 0,
			token : $.cookie("token")
		},
		cache : false,
		success : function(data) {
			if (data) {
				var provinceHtml = ohtml;
				var result = eval('(' + data + ')');
				for (var i = 0; i < result.length; i++) {
					var area = result[i];
					provinceHtml += "<option value='" + area.id + "'>" + area.name + "</option>";
				}
				$("#provinceId").html(provinceHtml);
				if (callback) {
					callback();
				}
			}
		},
		error : function(XMLHttpResponse) {
		}
	});
}

function loadCity(callback) {
	var proEl = $("#provinceId option:selected");
	var provinceId = proEl.val();
	var provinceName = proEl.text();
	if (provinceId > 0) {
		$.ajax({
			type : "post",
			dataType : "json",
			url : url,
			data : {
				parentId : provinceId,
				token : $.cookie("token")
			},
			cache : false,
			success : function(data) {
				var cityHtml = ohtml;
				if (data) {
					var result = eval('(' + data + ')');
					for (var i = 0; i < result.length; i++) {
						var area = result[i];
						cityHtml += "<option value='" + area.id + "'>" + area.name + "</option>";
					}
				}
				$("#cityId").html(cityHtml);
				$("#countyId").html(ohtml);
				$("#area").val(provinceName);
				if (callback) {
					callback();
				}
			},
			error : function(XMLHttpResponse) {
			}
		});
	} else {
		$("#cityId").html(ohtml);
		$("#countyId").html(ohtml);
		$("#area").val("");
	}
}

function loadCounty(callback) {
	var cityEl = $("#cityId option:selected");
	var cityId = cityEl.val();
	var provinceName = $("#provinceId option:selected").text();
	var cityName = cityEl.text();
	if (cityId > 0) {
		$.ajax({
			type : "post",
			dataType : "json",
			url : url,
			data : {
				parentId : cityId,
				token : $.cookie("token")
			},
			cache : false,
			success : function(data) {
				if (data) {
					var countyHtml = ohtml;
					var result = eval('(' + data + ')');
					for (var i = 0; i < result.length; i++) {
						var area = result[i];
						countyHtml += "<option value='" + area.id + "'>" + area.name + "</option>";
					}
					$("#countyId").html(countyHtml);
					$("#area").val(provinceName + " " + cityName);
					if (callback) {
						callback();
					}
				}
			},
			error : function(XMLHttpResponse) {
			}
		});
	} else {
		$("#countyId").html(ohtml);
		$("#area").val("");
	}
}
function edit(addressId){
	$.ajax({
		type : "post",
		url : appPath + "/address/orderEdit.htm",
		data : {
			addressid : addressId,
			token : $.cookie("token")
		},
		dataType : "html",
		success : function(result) {
			if(result.indexOf("loginpage") > -1){
				location.href = appPath + "/member/login.htm";
			}
			$("#popup1").html(result);
			$("#cover").show();
			$("#popup1").show();// 查找ID为popup的DIV show()显示#cover
			tc_center_center("popup1");
			addressReadyDo();
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			myalert("系统异常，请稍后再试");
		}
	});
}
function loadArea() {
	var province = $("#provinceId option:selected").val();
	var provinceName = $("#provinceId option:selected").text();
	if(province != '0'){
		$("#area").val(provinceName);
		var city = $("#cityId option:selected").val();
		var cityName = $("#cityId option:selected").text();
		if(city != '0'){
			$("#area").val(provinceName + " " + cityName);
			var county = $("#countyId option:selected").val();
			var countyName = $("#countyId option:selected").text();
			if(county != '0'){
				$("#area").val(provinceName + " " + cityName + " " + countyName);				
			}
		}
	}
}
function update() {
	var submitUrl =appPath + "/address/editsubmit.htm";
	var id = $("#id").val();
	var params = {
		name : $("#name").val(),
		mobPhone : $("#mobPhone").val(),
		area : $("#area").val(),
		address : $("#address").val(),
		token : $.cookie("token")
	};
	if(id){
		//submitUrl = appPath + "/address/editsubmit.html";
		params.id=id;
	}else{
		//submitUrl = appPath + "/address/addsubmit.html";
	}
	if($("#isDefault").val()){
		params.isDefault = $("#isDefault").val();
	}
	$("#msg").html("");
	if ($("#name").val() == '') {
		$("#msg").html("请输入收货人姓名");
		return false;
	}
	var mobileResult = checkMobile($("#mobPhone").val());
	if (!mobileResult.result) {
		$("#msg").html(mobileResult.msg);
		return false;
	}
	if ($("#provinceId").val() == 0 || $("#cityId").val() == 0) {
		$("#msg").html("请选择省市县区");
		return false;
	}
	if ($("#address").val() == '') {
		$("#msg").html("请输入详细地址");
		return false;
	}
	var setdefault = $("#setdefault").prop("checked");
	if (setdefault) {
		$("#isDefault").val('1');
	} else {
		$("#isDefault").val('0');
	}
	loadArea();
	$.ajax({
		type : "post",
		url : submitUrl,
		data : params,
		success : function(data) {
			$("a.guanbi").click();
			var addressInfoDiv = $(data).find("#address_div");
			$('#address_div').html($(addressInfoDiv[0]).html());
			addressReadyDo();
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			$("#msg").html("系统异常，请稍后再试");
        }
	});
}
function del(addressid) {
	myconfirm("确定要删除吗？", function() {
		$.showLoading();
		$.ajax({
			type : "post",
			url : appPath + "/address/orderSetDefaultAddress.htm",
			data : {
				operateType : "del",
				addressid : addressid,
				token : $.cookie("token")
			},
			success : function(data) {
				$.hideLoading();
				var addressInfoDiv = $(data).find("#address_div");
				$('#address_div').html($(addressInfoDiv[0]).html());
				addressReadyDo();
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				$.hideLoading();
	            $.alert("系统异常，请稍后再试");
	        }
		});
	})
}
function setDefault(addressid){
	$.showLoading();
	$.ajax({
		type : "post",
		url : appPath + "/address/orderSetDefaultAddress.htm",
		data : {
			operateType : "setDefault",
			addressid : addressid,
			token : $.cookie("token")
		},
		success : function(data) {
			$.hideLoading();
			var addressInfoDiv = $(data).find("#address_div");
			$('#address_div').html($(addressInfoDiv[0]).html());
			addressReadyDo();
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			$.hideLoading();
            $.alert("系统异常，请稍后再试");
        }
	});
}
//TODO
function toAdd() {
	$.ajax({
		type : "post",
		url : appPath + "/address/orderEdit.htm",
		data : {
			token : $.cookie("token")
		},
		dataType : "html",
		success : function(result) {
			if(result.indexOf("loginpage") > -1){
				location.href = appPath + "/member/login.htm";
			}
			$("#popup1").html(result);
			$("#cover").show();
			$("#popup1").show();// 查找ID为popup的DIV show()显示#cover
			tc_center_center("popup1");
			addressReadyDo();
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			myalert("系统异常，请稍后再试");
		}
	});
}
function confirmAddress(){
	if($("#addressIsNull").val()=="1"){
		$.alert("请添加收货地址！");
		return;
	}
	if($("input[name='addressId']:checked").length==0){
		$.alert("请选择收货地址！");
		return;
	}
	var selectedAddress = $("input[name='addressId']:checked");
	$("#address_info").text($($(selectedAddress).parent().find("p")[0]).text());
	$("#confirm_address_div").show();
	$("#choose_address_div").hide();
}
function showMoreAddress(){
	$('.more_address').show();
	$("#hide_more").show();
	$("#show_more").hide();
}
function hideMoreAddress(){
	$('.more_address').hide();
	$("#show_more").show();
	$("#hide_more").hide();
}
function addressReadyDo() {
	// 点击关闭按钮
	$("a.guanbi").click(function() {
		$("#cover").hide();
		$("#popup1").hide();// 查找ID为popup的DIV hide()隐藏
	})
	$("#msg").html("");
	loadProvince(function() {
		var provinceId = $("#province").val();
		if (provinceId == '' || provinceId == null) {
			$("#provinceId").val("0");
		} else {
			$("#provinceId").val(provinceId);
		}
		loadCity(function() {
			var cityId = $("#city").val();
			if (cityId == '' || cityId == null) {
				$("#cityId").val("0");
			} else {
				$("#cityId").val(cityId);
			}
			loadCounty(function() {
				var countyId = $("#county").val();
				if (countyId == '' || countyId == null) {
					$("#countyId").val("0");
				} else {
					$("#countyId").val(countyId);
				}
			});
		});
	});
}